﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.SceneManagement;

public static class SceneManagerUtility
{
    private static Dictionary<string, GameObject> singletonLookup = new Dictionary<string, GameObject>();

    [DebuggerHidden]
    private static IEnumerator _LoadSceneAsGameObject(string name, Action<GameObject> rootCallback = null)
    {
        return new <_LoadSceneAsGameObject>c__Iterator28 { name = name, rootCallback = rootCallback, <$>name = name, <$>rootCallback = rootCallback };
    }

    [DebuggerHidden]
    private static IEnumerator _LoadSceneAsSingleton(string name, Action<GameObject> rootCallback = null)
    {
        return new <_LoadSceneAsSingleton>c__Iterator29 { name = name, rootCallback = rootCallback, <$>name = name, <$>rootCallback = rootCallback };
    }

    public static Coroutine LoadSceneAsGameObject(MonoBehaviour coroutineHandler, string name, Action<GameObject> rootCallback = null)
    {
        return coroutineHandler.StartCoroutine(_LoadSceneAsGameObject(name, rootCallback));
    }

    public static Coroutine LoadSceneAsSingleton(MonoBehaviour coroutineHandler, string name, Action<GameObject> rootCallback = null)
    {
        return coroutineHandler.StartCoroutine(_LoadSceneAsSingleton(name, rootCallback));
    }

    public static void UnloadSingletonScene(string name)
    {
        if (singletonLookup.ContainsKey(name))
        {
            UnityEngine.Object.Destroy(singletonLookup[name]);
        }
    }

    [CompilerGenerated]
    private sealed class <_LoadSceneAsGameObject>c__Iterator28 : IDisposable, IEnumerator, IEnumerator<object>
    {
        internal object $current;
        internal int $PC;
        internal string <$>name;
        internal Action<GameObject> <$>rootCallback;
        internal GameObject[] <$s_133>__2;
        internal int <$s_134>__3;
        internal GameObject <child>__4;
        internal GameObject <root>__1;
        internal Scene <scene>__0;
        internal string name;
        internal Action<GameObject> rootCallback;

        [DebuggerHidden]
        public void Dispose()
        {
            this.$PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 0:
                    this.$current = SceneManager.LoadSceneAsync(this.name, LoadSceneMode.Additive);
                    this.$PC = 1;
                    return true;

                case 1:
                    this.<scene>__0 = SceneManager.GetSceneByName(this.name);
                    this.<root>__1 = new GameObject(this.name);
                    this.<$s_133>__2 = this.<scene>__0.GetRootGameObjects();
                    this.<$s_134>__3 = 0;
                    while (this.<$s_134>__3 < this.<$s_133>__2.Length)
                    {
                        this.<child>__4 = this.<$s_133>__2[this.<$s_134>__3];
                        this.<child>__4.transform.SetParent(this.<root>__1.transform);
                        this.<$s_134>__3++;
                    }
                    if (this.rootCallback != null)
                    {
                        this.rootCallback(this.<root>__1);
                    }
                    SceneManager.MergeScenes(this.<scene>__0, SceneManager.GetActiveScene());
                    this.$PC = -1;
                    break;
            }
            return false;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }
    }

    [CompilerGenerated]
    private sealed class <_LoadSceneAsSingleton>c__Iterator29 : IDisposable, IEnumerator, IEnumerator<object>
    {
        internal object $current;
        internal int $PC;
        internal string <$>name;
        internal Action<GameObject> <$>rootCallback;
        internal GameObject <singleton>__0;
        internal string name;
        internal Action<GameObject> rootCallback;

        internal void <>m__18B(GameObject x)
        {
            this.<singleton>__0 = x;
        }

        [DebuggerHidden]
        public void Dispose()
        {
            this.$PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 0:
                    this.<singleton>__0 = null;
                    SceneManagerUtility.singletonLookup.TryGetValue(this.name, out this.<singleton>__0);
                    if (this.<singleton>__0 != null)
                    {
                        break;
                    }
                    this.$current = SceneManagerUtility._LoadSceneAsGameObject(this.name, new Action<GameObject>(this.<>m__18B));
                    this.$PC = 1;
                    return true;

                case 1:
                    UnityEngine.Object.DontDestroyOnLoad(this.<singleton>__0);
                    break;

                default:
                    goto Label_00BD;
            }
            SceneManagerUtility.singletonLookup[this.name] = this.<singleton>__0;
            if (this.rootCallback != null)
            {
                this.rootCallback(this.<singleton>__0);
            }
            this.$PC = -1;
        Label_00BD:
            return false;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }
    }
}

